#include "tank.h"

using namespace sf;
using namespace std;

Tank::Tank(sf::Vector2f position, std::string image, sf::IntRect subVehicule,
            sf::IntRect subCanon, sf::Vector2f positioncanon
            ,sf::Vector2f positioncanonflip, sf::Vector2f centre, Roquette roquette,
             int equipe) : Unite(position,image,subVehicule,equipe)
{
    Option m_option;
    m_volume = 0;
    dead = false;
    m_type = TANK;
    m_physique = false;
    m_base.SetScale(0.5,0.5);
    m_canon.SetCaracter(positioncanon, positioncanonflip, image, subCanon, roquette);
    if(!m_buffercanon.LoadFromFile("sounds/tourelle.ogg"))
        cout << "Erreur dans le chargement du son du canon" << endl;
    else
        m_soundcanon.SetBuffer(m_buffercanon);
    if(!m_buffertank.LoadFromFile("sounds/tank.ogg"))
        cout << "Erreur dans le chargement du son du tank" << endl;
    else
        m_soundtank.SetBuffer(m_buffertank);

    m_soundcanon.SetVolume(m_option.GetVolume()/3);
    m_soundcanon.SetLoop(true);
    m_soundtank.SetVolume(0);
    m_soundtank.SetLoop(true);
}
Tank::~Tank()
{

}
void Tank::Afficher(sf::RenderWindow *app, sf::Vector2f vent, Terrain *terre, Game *jeu, vector<sf::Sprite> unite, bool visible)
{
        if(!m_sound)
        {
            m_soundcanon.Stop();
            m_soundtank.Stop();
        }
        m_sound = false;
        m_canon.Rafraichir(m_position, m_angle, flip);
        m_canon.DeplacerRoquette(vent, terre, jeu, unite);
        m_base.SetRotation(m_angle);
        m_canon.Afficher(app, terre, visible);
        if(visible)
        {
            app->Draw(m_base);
            m_spriteequipe.SetPosition(m_position.x, m_position.y-m_base.GetSize().y-40);
            m_spritevie.SetPosition(m_position.x-30, m_position.y-m_base.GetSize().y-25);
            app->Draw(m_spritevie);
            app->Draw(m_spriteequipe);
        }
}
void Tank::ajouterAngle()
{
    if(flip)
    {
        if(m_canon.GetAngle() >= 135)
        {
            m_canon.ChangerAngle(-1,0);
        }
        else
        {
            m_canon.ChangerAngle(135,1);
        }
    }
    else
    {
        if(m_canon.GetAngle() <= 45)
        {
            m_canon.ChangerAngle(1,0);
        }
        else
        {
            m_canon.ChangerAngle(45,1);
        }
    }
}
void Tank::retirerAngle()
{
    if(flip)
    {
        if(m_canon.GetAngle() <= 190)
        {
            m_canon.ChangerAngle(1,0);
        }
        else
        {
            m_canon.ChangerAngle(190,1);
        }
    }
    else
    {
        if(m_canon.GetAngle() >= -10)
        {
            m_canon.ChangerAngle(-1,0);
        }
        else
        {
            m_canon.ChangerAngle(-10,1);
        }
    }
}
void Tank::avancer(Terrain *terre)
{
    m_base.FlipX(0);
    if(flip)
    {
        m_canon.Flip(0);
    }
    flip = false;
    if(m_position.y - terre->cherTerrain(m_position.x+m_deplacement,m_position.y) < 20 && m_position.x+m_deplacement+20 < terre->GetTextureSize().x)
    {
        m_position.x += m_deplacement+bonus_deplacement;
    }
}
void Tank::reculer(Terrain *terre)
{
    m_base.FlipX(1);
    if(!flip)
    {
        m_canon.Flip(1);
    }
    flip = true;
 //   m_centre.x = m_canon.GetSize().x-m_canon.GetCenter().x;
 //   m_centre.y = m_canon.GetCenter().y;
  //  m_canon.setOrigin(m_centre);
    if(m_position.y - terre->cherTerrain(m_position.x-m_deplacement,m_position.y) < 20 && m_position.x-m_deplacement-20 >0)
    {
        m_position.x -= m_deplacement+bonus_deplacement;
        m_position.y = terre->cherTerrain(m_position.x,m_position.y);
    }
}
void Tank::tirer(sf::Vector2f vent)
{
    enTire = true;
    m_canon.Tir(m_angle, m_puissance, m_position, vent, flip);
}
Tank& Tank::operator=(const Tank &tank2)
{
    m_base = tank2.m_base;
    m_canon = tank2.m_canon;
    m_position = tank2.m_position;
    m_taille = tank2.m_taille;
    m_largeur = tank2.m_largeur;
    m_puissancemax = tank2.m_puissancemax;
    m_deplacement = tank2.m_deplacement;
    m_viemax = tank2.m_viemax;
    ia = tank2.ia;

}
bool Tank::Evenement(int input, sf::RenderWindow *app, Terrain *m_terre, Game *jeu)
{
    Option m_option;
    m_sound = true;
    if(input == 1)
    {
        if(m_soundcanon.GetStatus() == sf::Sound::Stopped)
        {
            m_soundcanon.Play();
        }
        ajouterAngle();
    }

    if(input == 2)
    {
        if(m_soundcanon.GetStatus() == sf::Sound::Stopped)
        {
            m_soundcanon.Play();
        }
        retirerAngle();
    }
    if(input == -1)
    {
        m_soundcanon.Stop();
    }

    if(input == 3)
    {
        if(m_soundtank.GetStatus() == sf::Sound::Stopped)
        {
            m_soundtank.Play();
        }
        avancer(m_terre);
    }

    if(input == 4)
    {
        if(m_soundtank.GetStatus() == sf::Sound::Stopped)
        {
            m_soundtank.Play();
        }
        reculer(m_terre);
    }

    if(input == 6)
    {
        m_soundtank.Stop();
        m_volume = 0;
        m_soundtank.SetVolume(m_volume);
    }
    if(m_soundtank.GetStatus() == Sound::Playing && m_volume < m_option.GetVolume()/2)
    {
        m_volume++;
        m_soundtank.SetVolume(m_volume);
    }
    if(input == 5)
        tirer(sf::Vector2f(0,0));

    if(input > 5)
    {
        return 0;
    }

    return 1;
}
sf::Vector2f Tank::GetRoquettePosition()
{
    return m_canon.GetRoquettePosition();
}
